<template>
  <div class="pad-out-box">
    <el-row type="flex" justify="end" style="margin-bottom: 15px">
      <el-button type="primary" size="mini" @click="addFn">新增</el-button>
    </el-row>
    <div class="padding-left-right-10">
      <el-table :border="true" :data="tableData">
        <el-table-column align="left" label="楼栋名称" prop="spaceName" />
        <!-- <el-table-column align="center" label="楼栋城市" prop="landArea">
          <template slot-scope="scope">
            <span>{{ scope.row.provinceName + '-' +  scope.row.cityName + '-' + scope.row.areaName}}</span>
          </template>
        </el-table-column> -->
        <el-table-column align="center" label="楼栋地址" prop="address">
          <template slot-scope="scope">
            <span>{{ scope.row.address }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="goAddPage(scope.row)">编辑</el-button>
            <!-- <el-button type="text" class="color-red" @click="deleteFn(scope.row)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <Pagination @change="pageChange" :total="total" />
    </div>
    <!-- 上传 -->
    <el-dialog title="楼栋信息" :visible.sync="addDialog" width="90%" :close-on-click-modal="false" :close-on-press-escape="false">
      <form-common :filed-list="buildFiledList" @remoteMethod="remoteMethod" @selectChange="selectChange" ref="form" @rightBtnFn="rightBtnFn"></form-common>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="addDialog = false">取消</el-button>
        <el-button size="small" type="primary" @click="confirmAddBuild">确定</el-button>
      </span>
    </el-dialog>
    <AddressBMap :mapVisible="mapDialog" v-on:mapLocationClose="mapDialog = false" v-on:mapLocationSave="mapLocationSave"></AddressBMap>
  </div>
</template>
<script>
import formCommon from '@g/components/form/formCommon.vue';
import { getMenuList } from '@/api';
import { dictList, provinceList, cityList, areaList } from '../../api/common';
import AddressBMap from '@/common/components/AddressBMap';
import { buildFiledList } from './form.js';
import Api from '../../api/projectInfo';
import { requestTips, askDialog } from '../../assets/util';

export default {
  data() {
    return {
      buildFiledList: buildFiledList,
      tableData: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      addDialog: false,
      mapDialog: false,
      currentFiledItem: {},
      userInfo: JSON.parse(localStorage.getItem('userInfo')),
    };
  },

  mounted() {
    this.getTableData();
    this.getDictionary();
    this.getProvinceList();
    this.getassetpackagelist();
    this.getOrgList()
  },

  methods: {
      //获取运营团队
    async getOrgList() {
      let res = await Api.apiProjectCorrelation({
        orgProjectType: 'ziguanProject',
      });
      this.buildFiledList.forEach(item => {
        if (item.bindFieldName == 'organizationKey') {
          item.optionList = res.rows || [];
        }
      });
    },
    //字典
    async getDictionary() {
      let res = await dictList({
        dictType: 'land_type',
      });
      this.buildFiledList.forEach(item => {
        if (item.bindFieldName == 'landType') {
          item.optionList = res.rows || [];
        }
      });
    },
    //资产包
    async getassetpackagelist() {
      Api.assetpackagelist({}).then(res => {
        this.buildFiledList.forEach(item => {
          if (item.bindFieldName == 'packageKey') {
            item.optionList = res.rows || [];
          }
        });
      });
    },
    // 获取省
    async getProvinceList() {
      let res = await provinceList({});
      this.buildFiledList.forEach(item => {
        if (item.bindFieldName == 'provinceCode') {
          item.optionList = res.rows || [];
        }
      });
    },
    // 获取市
    async getCityList(provinceCode) {
      let res = await cityList({ provinceCode: provinceCode });
      this.buildFiledList.forEach(item => {
        if (item.bindFieldName == 'cityCode') {
          item.optionList = res.rows || [];
        }
      });
    },
    // 获取区
    async getAreaList(cityCode) {
      let res = await areaList({ cityCode: cityCode });
      this.buildFiledList.forEach(item => {
        if (item.bindFieldName == 'areaCode') {
          item.optionList = res.rows || [];
        }
      });
    },
    // 切换
    selectChange(data) {
      if (data.filedName == 'provinceCode') {
        this.$refs['form'].setFiledSource('cityCode', '');
        this.$refs['form'].setFiledSource('areaCode', '');
        this.buildFiledList.forEach(item => {
          if (item.bindFieldName == 'areaCode') {
            item.optionList = [];
          }
        });
        this.getCityList(data.filedVal);
      } else if (data.filedName == 'cityCode') {
        this.$refs['form'].setFiledSource('areaCode', '');

        this.getAreaList(data.filedVal);
      }
    },
    remoteMethod(val) {
      Api.managerlist({ employeeName: val.remoteVal }).then(res => {
        if (res.success) {
          val.cbList(val.formItem.bindFieldName, res.rows);
        }else{
          val.cbList(val.formItem.bindFieldName, []);
        }
      });
    },
    //数据源
    async getTableData(type) {
      // todo
      let res = await Api.projectListNew({
        currentPage: type ? this.currentPage : 1,
        pageSize: this.pageSize,
        manager: this.$route.query.from?null:this.userInfo.key,
        status:1
      });
      this.tableData = res.rows || [];
      this.total = res.total;
    },
    //起草
    Drafting() {
      let pathUrl = {
        name: 'contract-drafting',
      };
      let { href } = this.$router.resolve(pathUrl);
      window.open(href);
    },
    //分页
    pageChange(e) {
      this.currentPage = e.currentPage;
      this.pageSize = e.pageSize;
      this.getTableData('more');
    },
    //新增
    addFn() {
      this.getTableData();
      this.addDialog = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidateForm();
      });
    },
    async confirmAddBuild() {
      // todo
      let model = this.$refs.form.validateForm();
      if (!model) return;
      model.latitude = model.lat;
      model.longitude = model.lng;
      let res = await Api.apiProjectAddNew(model);
      requestTips(res, () => {
        this.getTableData();
        this.addDialog = false;
      });
    },
    //点击地图回掉
    rightBtnFn(data) {
      // console.log(data);
      this.currentFiledItem = data;
      this.mapDialog = true;
    },
    //小区地址选择回调
    mapLocationSave(e) {
      this.currentFiledItem.cb(this.currentFiledItem.filedName, e.address);
      this.currentFiledItem.model.lng = e.longitude;
      this.currentFiledItem.model.lat = e.latitude;
      this.mapDialog = false;
    },
    //编辑
    async goAddPage(row) {
      window.open(`/businessAssets/building/info?menuId=${this.$route.query.menuId}&projectKey=${row.projectKey}&spaceKey=${row.spaceKey}`, row.spaceKey)
    },
  },

  components: { formCommon, AddressBMap },
};
</script>
<style scoped></style>
